import LinvoDB from './api'
var modelName = "student";
var schema = { }; // Non-strict always, can be left empty
var options = { };

//创建数据表
// let studentDB = new LinvoDB(modelName, schema,options)

export default class StudentDao {
    //数据库操作工具方法
    static insertData(data) {
        return new Promise((resolve, reject) => {
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.save(data,function (err,newInvoice) {
                // console.log(666,newInvoice)
                resolve(newInvoice)
            })
        })
    }

    static getDetail(data) {
        return new Promise((resolve, reject) => {
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.findOne(data,function (err,result) {
                // console.log(666,newInvoice)
                resolve(result)
            })
        })
    }

    static findListNoPage(data,sort) {
        return new Promise((resolve, reject) => {
            if(!sort) sort = {"createAt":-1}
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.find(data).sort(sort)
                .exec(function (err, results) {
                    resolve(results);
                });
        })
    }

    static findList(params) {
        let data = params.searchKey,startPages = params.page,pageSize = params.pageSize,sort = params.sort
        return new Promise((resolve, reject) => {
            if(!sort) sort = {"createAt":-1}
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.find(data).sort(sort)
                .skip(startPages).limit(pageSize)
                .exec(function (err, results) {
                    resolve(results);
                });
        })
    }

    static removeData(data) {
        return new Promise((resolve, reject) => {
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.remove(data,{ multi: true },function (err,newInvoice) {
                // console.log(666,newInvoice)
                resolve(err)
            })
        })
    }

    static updateData(query,data) {
        return new Promise((resolve, reject) => {
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.update(query,{ $set: data},{},function (err,numReplaced) {
                // console.log(666,newInvoice)
                resolve(err)
            })
        })
    }

    static count(searchParam) {
        return new Promise((resolve, reject) => {
            let studentDB = new LinvoDB(modelName, schema,options)
            studentDB.count(searchParam,function (err, count) {
                resolve(count);
            })
        })
    }
}
